package com.aier.admin.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.aier.admin.entity.AdminParenting;
import com.aier.admin.entity.mapper.AdminParentingRowMapper;
import com.aier.admin.service.IAdminParentingService;
import com.aier.common.common.Constant;
import com.aier.common.dao.ICommonDAO;
import com.aier.proxy.service.IProxyService;

@Component("adminParentingService")
public class AdminParentingServiceImpl_J implements IAdminParentingService {

    @Resource(name="commonDao")
    private ICommonDAO commonDao;
    public void setCommonDao(ICommonDAO commonDao) {
        this.commonDao = commonDao;
    }
    
    @Override
    public Map<String, Object> searchParenting(int page, int page_size,
            int sortable) {
        String sql = "select * from sys_parenting p";
        
        switch (sortable) {
            case IProxyService.ENTRANCE_SORTABLE_SN:
                sql += " order by p.days";
                break;
            case IProxyService.ENTRANCE_SORTABLE_SN_DESC:
                sql += " order by p.days";
                break;
        }
        return commonDao.queryPage(sql,
                    page, page_size, new Object[] {},
                     new AdminParentingRowMapper());
    }

    @Override
    public int insertParenting(Long days, String content, String filename) {
        if (null != commonDao.listByObject("select * from sys_parenting where days=?",
                new Object[] {days}, new AdminParentingRowMapper())) {
            return Constant.num_10000;
        }
        
        if (commonDao.update("insert into sys_parenting(days,content,image) value(?,?,?)",
                new Object[] {days,content,filename})>0) {
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public AdminParenting searchPatentingInfo(Long spid) {
        return (AdminParenting) commonDao.listByObject("select * from sys_parenting where spid=?",
                new Object[] {spid}, new AdminParentingRowMapper());
    }

    @Override
    public int updateParenting(Long spid, Long days, String content, String filename) {
        AdminParenting adminParenting = (AdminParenting) commonDao.listByObject("select * from sys_parenting where days=?",
                new Object[] {days}, new AdminParentingRowMapper());
        if (!adminParenting.getSpid().equals(spid)) {
            return Constant.num_10000;
        }
        if (commonDao.update("update sys_parenting set days=?,content=?,image=? where spid=?",
                new Object[] {days, content,filename,spid }) > 0) {
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public AdminParenting searchPatentingByDays(Long days) {
        return (AdminParenting) commonDao.listByObject("select * from sys_parenting where days=?",
                new Object[] {days}, new AdminParentingRowMapper());
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<AdminParenting> searchPatentingFavorite(Long uid, long mid) {
        return (List<AdminParenting>)commonDao.list("select sp.*,uf.description as ageInfo from sys_parenting sp " +
        		"left join userapp_favorite uf on sp.spid=uf.contentid where uf.uid =? and mid=?",
                new Object[] {uid,mid}, new AdminParentingRowMapper());
    }    


}
